package com.ligy.ui;

import com.ligy.user.User;
import com.ligy.user.UserManage;
import com.ligy.utils.JFrameUtils;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;
import java.util.Random;

public class LoginJFrame extends JFrame implements MouseListener {
    public LoginJFrame() {
        initFrame();

        initView();
    }

    private JTextField usernameInput;
    private JPasswordField passwordInput;

    /**
     * 初始化框架
     */
    private void initFrame() {
        this.setSize(488, 430);

        this.setTitle("拼图小游戏 登录");

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLocationRelativeTo(null);

        this.setAlwaysOnTop(true);

        this.setVisible(true);
    }

    private void initView() {
        java.awt.Container container = this.getContentPane();

        // 添加用户名
        JFrameUtils.loadJLabelImage(container, "image/login/用户名.png", 80, 100, 100, 100);

        // 添加密码
        JFrameUtils.loadJLabelImage(container, "image/login/密码.png", 80, 150, 100, 100);

        // 添加验证码
        JFrameUtils.loadJLabelImage(container, "image/login/验证码.png", 80, 200, 100, 100);

        // 登录按钮
        JLabel logiNBtn = JFrameUtils.loadJLabelImage(container, "image/login/登录按钮.png", 90, 280, 128, 47);
        logiNBtn.addMouseListener(this);

        // 注册按钮
        JFrameUtils.loadJLabelImage(container, "image/login/注册按钮.png", 280, 280, 128, 47);

        // 添加用户名输入框
        usernameInput = JFrameUtils.createInput(container, 180, 140, 180, 20);

        // 添加密码输入框
        passwordInput = JFrameUtils.createPassInput(container, 180, 190, 180, 20);

        // 添加验证码输入框
        JFrameUtils.createInput(container, 180, 240, 100, 20);

        //生成随机字符串
        String randomStr = "";
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            char s = (char) random.nextInt(97, 123);
            randomStr += s;
        }
        JFrameUtils.createText(container, randomStr, 300, 240, 100, 20);

        // 添加背景图
        JFrameUtils.loadJLabelImage(container, "image/login/background.png", 0, 0, 470, 390);
        this.getContentPane().repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        getFormData();
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    private void getFormData() {
        String username = usernameInput.getText();
        String password = String.valueOf(passwordInput.getPassword());
        if (username.isEmpty() || password.isEmpty()) {
            return;
        }

        boolean res = new UserManage().login(new User(username, password));
        if (res) {
            new GameFrame();
            this.setVisible(false);
        } else {
            System.out.println("登陆失败");
        }
    }
}
